// service.js文件
import Vue from 'vue'
import axios from 'axios'
import https from '@/config/index.config.js'
let baseURL = https.baseUrl
const service = axios.create({
	withCredentials: true,
	crossDomain: true,
	baseURL,
	timeout: 6000
})
//配置成功后的拦截器
service.interceptors.response.use(res => {
	if (res.data.data) {
		return res.data
	} else {
		return res.data
		// uni.reLaunch({
		// 	url:'/pages/stopPage/stopPage'
		// })
		//       return Promise.reject(res.data.msg);
	}
}, error => {
	return Promise.reject(error)
})


axios.defaults.adapter = function(config) { //自己定义个适配器，用来适配uniapp的语法
	return new Promise((resolve, reject) => {
		console.log(uni.getStorageSync('token'),'999')
		let token = {
			token: uni.getStorageSync("token")
		}
		let obj = {
			...token,
			...config.headers
		};
		var settle = require('axios/lib/core/settle');
		var buildURL = require('axios/lib/helpers/buildURL');
		uni.request({
			method: config.method.toUpperCase(),
			url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
			header: obj,
			data: config.data,
			dataType: config.dataType,
			responseType: config.responseType,
			sslVerify: config.sslVerify,
			complete: function complete(response) {
				response = {
					data: response.data,
					status: response.statusCode,
					errMsg: response.errMsg,
					header: response.header,
					config: config
				};
				settle(resolve, reject, response);
			}
		})
	})
}
export default service